﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using Microsoft.Win32;

namespace BDSim
{
    #region 存储文件的轨迹名（与需求方一致）
    /// <summary>
    /// 飞机指令类型
    /// </summary>
    public enum AircraftSimTrackNameFileSave
    {
        Ref,
        AirLine,
        AirAcc,
        AirClimb,
        AirTurn,
        AirTurnAcc,
        AirWayPoint,
        AirCombined,
        AirLinePitch,
        AirLineRoll,
        AirLineYaw,
        AirHalt
    }

    /// <summary>
    /// 汽车指令类型
    /// </summary>
    public enum CarSimTrackNameFileSave
    {
        Ref,//起始
        CarLineConst,//直线稳速
        CarLineAcc,//加速直线
        CarTurnConst,//稳速转弯
        CarPoint,//定点行驶
        CarClimb,//爬升运动
        CarHalt//停止运动
    }

    /// <summary>
    /// 船指令类型
    /// </summary>
    public enum BoatSimTrackNameFileSave
    {
        Ref,
        BoatLineConst,
        BoatLineAcc,
        BoatTurnConst,
        BoatPoint,
        BoatHalt
    }

    /// <summary>
    /// 要保存的文件格式名称
    /// </summary>
    public enum LimTrackNameFileSave
    {
        Line_spend_max,
        Line_acc_max,
        Line_jerk_max,
        Lateral_acc_max,
        Lateral_jerk_max,
        Yaw_rat_max,
        Yaw_acc_max,
        Yaw_jerk_max,
        Pitch_angle_max,
        Pitch_rat_max,
        Pitch_acc_max,
        Pitch_jrek_max,
        Roll_angle_max,
        Roll_rate_max,
        Roll_acc_max,
        Roll_jerk_max,
        Simustep,
        Outstep
    }
    #endregion 存储文件的轨迹名（与需求方一致）

    public class AircraftDynamicSetViewModel : INotifyPropertyChanged
    {
        #region///常量
        const double PI = 3.1415926535898;
        const double RATDEG = PI / 180.0;
        #endregion

        #region 属性
        private UserBaseModel _parent;

        /// <summary>
        /// 窗体所属的父节点
        /// </summary>
        public UserBaseModel Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        public string SimTrackFileName;
        #endregion 属性

        public AircraftDynamicSetViewModel()
        {
            InitCommands();
        }

        #region ///轨迹模型列表
        ObservableCollection<BaseTrackModel> _TrackModelList = new ObservableCollection<BaseTrackModel>();

        public ObservableCollection<BaseTrackModel> TrackModelList
        {
            get { return _TrackModelList; }
            set { _TrackModelList = value; NotifyPropertyChanged("TrackModelList"); }
        }
        #endregion

        #region 极限值设定
        /// <summary>
        /// 直线动态限值
        /// </summary>
        private LineRunLimt _LineRunLimtValue = new LineRunLimt() { MaxVel = 200.3658945844125520, MaxAccelarate = 50.0, MaxAcceAcce = 500 };

        public LineRunLimt LineRunLimtValue
        {
            get { return _LineRunLimtValue; }
            set { _LineRunLimtValue = value; NotifyPropertyChanged("LineRunLimtValue"); }
        }
        /// <summary>
        /// 航向角限值
        /// </summary>
        private OritatonAngleLimt _OritatonAngleLimtValue = new OritatonAngleLimt() { MaxAngleVel = 1, MaxAngleAccelarate = 10.0, MaxAngleAcceAcce = 100 };

        public OritatonAngleLimt OritatonAngleLimtValue
        {
            get { return _OritatonAngleLimtValue; }
            set { _OritatonAngleLimtValue = value; NotifyPropertyChanged("OritatonAngleLimtValue"); }
        }
        /// <summary>
        /// 俯仰角限值
        /// </summary>
        private PitchLimt _PitchLimtValue = new PitchLimt() { MaxAngle = 1.5707963267949, MaxAngleVel = 1, MaxAngleAccelarate = 10, MaxAngleAcceAcce = 100 };

        public PitchLimt PitchLimtValue
        {
            get { return _PitchLimtValue; }
            set { _PitchLimtValue = value; NotifyPropertyChanged("PitchLimtValue"); }
        }
        /// <summary>
        /// 横滚角限值
        /// </summary>
        private LandRollLimt _LandRollLimtValue = new LandRollLimt() { MaxAngle = 3.14159265358979, MaxAngleVel = 1, MaxAngleAccelarate = 10, MaxAngleAcceAcce = 100 };

        public LandRollLimt LandRollLimtValue
        {
            get { return _LandRollLimtValue; }
            set { _LandRollLimtValue = value; NotifyPropertyChanged("LandRollLimtValue"); }
        }
        /// <summary>
        /// 侧向动态限值
        /// </summary>
        private LateralLimt _LateralLimtValue = new LateralLimt() { MaxAccelarate = 69.3092, MaxAcceAcce = 500 };

        public LateralLimt LateralLimtValue
        {
            get { return _LateralLimtValue; }
            set { _LateralLimtValue = value; NotifyPropertyChanged("LateralLimtValue"); }
        }

        private int _SimStepSelectedIndex = 4;

        public int SimStepSelectedIndex
        {
            get { return _SimStepSelectedIndex; }
            set { _SimStepSelectedIndex = value; NotifyPropertyChanged("SimStepSelectedIndex"); }
        }
        private int _OutStepSelectedIndex = 4;

        public int OutStepSelectedIndex
        {
            get { return _OutStepSelectedIndex; }
            set { _OutStepSelectedIndex = value; NotifyPropertyChanged("OutStepSelectedIndex"); }
        }
        /// <summary>
        /// 仿真步长
        /// </summary>
        private int[] _SimStepArry = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        public int[] SimStepArry
        {
            get { return _SimStepArry; }
            set { _SimStepArry = value; }
        }
        /// <summary>
        /// 输出步长
        /// </summary>
        private int[] _OutStepArry = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        public int[] OutStepArry
        {
            get { return _OutStepArry; }
            set { _OutStepArry = value; }
        }

        #endregion

        #region//接口方法
        public void AddTrackModel(BaseTrackModel trackModel)
        {
            TrackModelList.Add(trackModel);
        }

        public void DeleteTrackModel(Guid uid)
        {
            string TrackId = uid.ToString();
            foreach (var item in TrackModelList)
            {
                if (item.UID == TrackId)
                {
                    TrackModelList.Remove(item);
                    break;
                }
            }
        }

        public void SaveTrackLimitValueFile(string FilePath, string FileName)
        {
            FilePath += "lim";
            FileName += "lim";
            try
            {
                string strTemp;
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Line_spend_max.ToString(), LineRunLimtValue.MaxVel);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Line_acc_max.ToString(), LineRunLimtValue.MaxAccelarate);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Line_jerk_max.ToString(), LineRunLimtValue.MaxAcceAcce);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Lateral_acc_max.ToString(), LateralLimtValue.MaxAccelarate);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Lateral_jerk_max.ToString(), LateralLimtValue.MaxAcceAcce);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Yaw_rat_max.ToString(), OritatonAngleLimtValue.MaxAngleVel);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Yaw_acc_max.ToString(), OritatonAngleLimtValue.MaxAngleAccelarate);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Yaw_jerk_max.ToString(), OritatonAngleLimtValue.MaxAngleAcceAcce);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Pitch_angle_max.ToString(), PitchLimtValue.MaxAngle);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Pitch_rat_max.ToString(), PitchLimtValue.MaxAngleVel);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Pitch_acc_max.ToString(), PitchLimtValue.MaxAngleAccelarate);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Pitch_jrek_max.ToString(), PitchLimtValue.MaxAngleAcceAcce);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Roll_angle_max.ToString(), LandRollLimtValue.MaxAngle);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Roll_rate_max.ToString(), LandRollLimtValue.MaxAngleVel);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Roll_acc_max.ToString(), LandRollLimtValue.MaxAngleAccelarate);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Roll_jerk_max.ToString(), LandRollLimtValue.MaxAngleAcceAcce);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Simustep.ToString(), SimStepSelectedIndex);
                File.AppendAllText(FilePath, strTemp);
                strTemp = string.Format("{0,-20},{1,-20}\n", LimTrackNameFileSave.Outstep.ToString(), OutStepSelectedIndex);
                File.AppendAllText(FilePath, strTemp);

            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show("保存限值设定文件出错！");
            }
        }

        #endregion

        #region ///命令初始化
        /// <summary>
        /// 命令初始化
        /// </summary>
        void InitCommands()
        {
            CmdTrackEdit.DlgExecuted = TrackEdit;
            //CmdInertiaParamSet.DlgExecuted = InertiaParamSet;

            LoadTrackCommand.DlgExecuted = LoadTrack;
            SaveTrackCommand.DlgExecuted = SaveTrack;
            //SimTrackCommand.DlgExecuted = SimTrack;
            //SetWaveGradeCommand.DlgExecuted = SetWaveGrade;
            SetTrackLimitCommand.DlgExecuted = SetTrackLimit;
            //CmdSaveLimtValue.DlgExecuted = SaveLimtValue;
        }
        #endregion

        #region 功能选择命令
        /// <summary>
        /// 轨迹编辑命令
        /// </summary>
        private TrackSimpleCommand _CmdTrackEdit = new TrackSimpleCommand();

        public TrackSimpleCommand CmdTrackEdit
        {
            get { return _CmdTrackEdit; }
            set { _CmdTrackEdit = value; }
        }

        /// <summary>
        /// 显示轨迹编辑窗口的命令实现
        /// </summary>
        /// <param name="param"></param>
        void TrackEdit(object param)
        {
            DlgDisplayTrackWnd();
        }

        #endregion 功能选择命令

        #region 工具栏命令定义
        /// <summary>
        /// 设置[设置轨迹限值]命令
        /// </summary>
        private TrackSimpleCommand _SetTrackLimitCommand = new TrackSimpleCommand();

        public TrackSimpleCommand SetTrackLimitCommand
        {
            get { return _SetTrackLimitCommand; }
            set { _SetTrackLimitCommand = value; }
        }
        #endregion 工具栏命令定义

        #region 工具栏命令实现
        /// <summary>
        /// 设置轨迹限值
        /// </summary>
        /// <param name="param"></param>
        void SetTrackLimit(object param)
        {


            TrackSetMaxValueWnd setMaxValueWnd = new TrackSetMaxValueWnd();
            #region 将SubWindow中的极限值传过来
            setMaxValueWnd.SubViewModel.LineRunLimtValue = this.LineRunLimtValue;
            setMaxValueWnd.SubViewModel.OritatonAngleLimtValue = this.OritatonAngleLimtValue;
            setMaxValueWnd.SubViewModel.PitchLimtValue = this.PitchLimtValue;
            setMaxValueWnd.SubViewModel.LandRollLimtValue = this.LandRollLimtValue;
            setMaxValueWnd.SubViewModel.LateralLimtValue = this.LateralLimtValue;
            setMaxValueWnd.SubViewModel.SimStepSelectedIndex = this.SimStepSelectedIndex;
            setMaxValueWnd.SubViewModel.OutStepSelectedIndex = this.OutStepSelectedIndex;
            #endregion

            if (setMaxValueWnd.ShowDialog() == true)
            {
                #region 将SubWindow中的极限值传过来
                this.LineRunLimtValue = setMaxValueWnd.SubViewModel.LineRunLimtValue;
                this.OritatonAngleLimtValue = setMaxValueWnd.SubViewModel.OritatonAngleLimtValue;
                this.PitchLimtValue = setMaxValueWnd.SubViewModel.PitchLimtValue;
                this.LandRollLimtValue = setMaxValueWnd.SubViewModel.LandRollLimtValue;
                this.LateralLimtValue = setMaxValueWnd.SubViewModel.LateralLimtValue;
                this.SimStepSelectedIndex = setMaxValueWnd.SubViewModel.SimStepSelectedIndex;
                this.OutStepSelectedIndex = setMaxValueWnd.SubViewModel.OutStepSelectedIndex;
                #endregion
            }
        }

        void SaveLimtValue(object param)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.OpenFile();
        }
        #endregion 工具栏命令实现

        #region 方法委托
        public delegate List<string> GetTrackIDorder();
        public GetTrackIDorder DlgGetTrackIDorder;

        public delegate void SaveScenXMLFile(string Path);
        public SaveScenXMLFile DlgSaveXMLFile;

        public delegate void LoadScenXMLFile(string Path);
        public LoadScenXMLFile DlgLoadXMLFile;
        /// <summary>
        /// 显示轨迹编辑窗口
        /// </summary>
        public delegate void DisplayTrackWnd();
        public DisplayTrackWnd DlgDisplayTrackWnd;
        
        #endregion

        #region ///仿真栏命令定义
        private TrackSimpleCommand _LoadTrackCommand = new TrackSimpleCommand();

        public TrackSimpleCommand LoadTrackCommand
        {
            get { return _LoadTrackCommand; }
            set { _LoadTrackCommand = value; }
        }

        private TrackSimpleCommand _SaveTrackCommand = new TrackSimpleCommand();

        public TrackSimpleCommand SaveTrackCommand
        {
            get { return _SaveTrackCommand; }
            set { _SaveTrackCommand = value; }
        }

        private TrackSimpleCommand _SimTrackCommand = new TrackSimpleCommand();

        public TrackSimpleCommand SimTrackCommand
        {
            get { return _SimTrackCommand; }
            set { _SimTrackCommand = value; }
        }
        #endregion

        #region ///仿真栏命令实现
        /// <summary>
        /// 加载轨迹文件
        /// </summary>
        void LoadTrack(object param)//?
        {
            OpenFileDialog LoadTrackFile = new OpenFileDialog();
            LoadTrackFile.Filter = "文本文档(*.xml)|*.xml|所有文件(*.*)|*.*";
            if (LoadTrackFile.ShowDialog() == true)
            {

                try
                {
                    DlgLoadXMLFile(LoadTrackFile.FileName);
                    
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.StackTrace, e.Message, MessageBoxButton.OK, MessageBoxImage.Error);
                }

            }
        }
        /// <summary>
        /// 保存轨迹文件
        /// </summary>
        void SaveTrack(object param)
        {
            SaveFileDialog SaveTrackFile = new SaveFileDialog();
            {
                SaveTrackFile.Filter = "文本文档(*.)|*.|所有文件(*.*)|*.*";
            }
            if (SaveTrackFile.ShowDialog() == true)
            {
                try
                {
                    #region 保存轨迹文件
                    string SaveName = SaveTrackFile.FileName;
                    string SavePath = SaveName;

                    SimTrackFileName = SavePath;//将该路径下的文件名传给载体用户

                    string SaveFileName = SaveName + "simu";
                    string SaveFilePath = SaveFileName;
                    string FlagModel = "";
                    string strtemp;
                    List<string> TrackIDOrderList;
                    TrackIDOrderList = DlgGetTrackIDorder();
                    for (int i = 0; i < TrackIDOrderList.Count; i++)
                    {
                        foreach (var item in TrackModelList)
                        {
                            if (item.UID == TrackIDOrderList[i].ToString())
                            {
                                FlagModel = item.DeviceModelType.ToString();
                                switch (item.DeviceTrackType.ToString())
                                {
                                    #region 汽车轨迹
                                    case "CarStartSetting":
                                        CarStartSetting carStartSet = new CarStartSetting();
                                        carStartSet = (CarStartSetting)item;

                                        strtemp = string.Format("{0},{1},{2},{3},{4},{5}\n", CarSimTrackNameFileSave.Ref.ToString(), Math.Round(carStartSet.Longitude * RATDEG, 15), Math.Round(carStartSet.Latitude * RATDEG, 15), carStartSet.Height, Math.Round(carStartSet.NavOrientation * RATDEG, 15), carStartSet.Velocity);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "CarSteadyVelocityLine":
                                        CarSteadyVelocityLine carSteadyVelocityLine = new CarSteadyVelocityLine();
                                        carSteadyVelocityLine = (CarSteadyVelocityLine)item;
                                        strtemp = string.Format("{0},{1},{2}\n", CarSimTrackNameFileSave.CarLineConst.ToString(), carSteadyVelocityLine.RunWay.RunSelect, carSteadyVelocityLine.RunWay.RunValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "CarAccelerateLine":
                                        CarAccelerateLine carAccelerateLine = new CarAccelerateLine();
                                        carAccelerateLine = (CarAccelerateLine)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4}\n", CarSimTrackNameFileSave.CarLineAcc.ToString(), carAccelerateLine.AccelerateWay.AccelerateSelect, carAccelerateLine.AccelerateWay.AccelerateValue, carAccelerateLine.VelChangeWay.VelChangeSelect, carAccelerateLine.VelChangeWay.VelChangeValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "CarSteadyVelSwerve":
                                        CarSteadyVelSwerve carSteadyVelSwerve = new CarSteadyVelSwerve();
                                        carSteadyVelSwerve = (CarSteadyVelSwerve)item;
                                        strtemp = string.Format("{0},{1},{2},{3}\n", CarSimTrackNameFileSave.CarTurnConst.ToString(), carSteadyVelSwerve.OrientChageWay.OrientationChangeSelect, Math.Round(carSteadyVelSwerve.OrientChageWay.OrientationChangeValue * RATDEG, 15), carSteadyVelSwerve.SwerveRadius);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "CarClimbRun":
                                        CarClimbRun carClimbRun = new CarClimbRun();
                                        carClimbRun = (CarClimbRun)item;
                                        strtemp = string.Format("{0},{1},{2},{3}\n", CarSimTrackNameFileSave.CarClimb.ToString(), carClimbRun.HeightChageWay.HeightChangeSelect, carClimbRun.HeightChageWay.HeightChangeValue, carClimbRun.RunDistance);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "CarDotRun":
                                        CarDotRun carDotRun = new CarDotRun();
                                        carDotRun = (CarDotRun)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4}\n", CarSimTrackNameFileSave.CarPoint.ToString(), Math.Round(carDotRun.Latitude * RATDEG, 15), Math.Round(carDotRun.Longitude * RATDEG, 15), carDotRun.Height, carDotRun.SwerveRadius);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "CarStopRun":
                                        CarStopRun carStopRun = new CarStopRun();
                                        carStopRun = (CarStopRun)item;
                                        strtemp = string.Format("{0},{1},{2}\n", CarSimTrackNameFileSave.CarHalt.ToString(), carStopRun.StopWay.StopSelect, carStopRun.StopWay.StopValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    #endregion

                                    #region 飞机轨迹
                                    case "AircraftStartSetting":
                                        AircraftStartSetting aircrftStartSet = new AircraftStartSetting();
                                        aircrftStartSet = (AircraftStartSetting)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4},{5}\n", AircraftSimTrackNameFileSave.Ref.ToString(), Math.Round(aircrftStartSet.Longitude * RATDEG, 15), Math.Round(aircrftStartSet.Latitude * RATDEG, 15), aircrftStartSet.Height, Math.Round(aircrftStartSet.NavOrientation * RATDEG, 15), aircrftStartSet.Velocity);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftSteadyVelLine":
                                        AircraftSteadyVelLine aircraftSteadyVelLine = new AircraftSteadyVelLine();
                                        aircraftSteadyVelLine = (AircraftSteadyVelLine)item;
                                        strtemp = string.Format("{0},{1},{2}\n", AircraftSimTrackNameFileSave.AirLine.ToString(), aircraftSteadyVelLine.RunWay.RunSelect, aircraftSteadyVelLine.RunWay.RunValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftAccelerateLine":
                                        AircraftAccelerateLine aircraftAccelerateLine = new AircraftAccelerateLine();
                                        aircraftAccelerateLine = (AircraftAccelerateLine)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4}\n", AircraftSimTrackNameFileSave.AirAcc.ToString(), aircraftAccelerateLine.AccelerateWay.AccelerateSelect, aircraftAccelerateLine.AccelerateWay.AccelerateValue, aircraftAccelerateLine.VelChangeWay.VelChangeSelect, aircraftAccelerateLine.VelChangeWay.VelChangeValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftClimbRun":
                                        AircraftClimbRun aircraftClimbRun = new AircraftClimbRun();
                                        aircraftClimbRun = (AircraftClimbRun)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4}\n", AircraftSimTrackNameFileSave.AirClimb.ToString(), aircraftClimbRun.DeltaHeight, aircraftClimbRun.DotHeight, aircraftClimbRun.StartAccelaration, aircraftClimbRun.StopAccelaration);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;

                                    case "AircraftSteadyVelSwerve":
                                        AircraftSteadyVelSwerve aircraftSteadyVelSwerve = new AircraftSteadyVelSwerve();
                                        aircraftSteadyVelSwerve = (AircraftSteadyVelSwerve)item;
                                        strtemp = string.Format("{0},{1},{2}\n", AircraftSimTrackNameFileSave.AirTurn.ToString(), Math.Round(aircraftSteadyVelSwerve.NavOrientChageValue * RATDEG, 15), aircraftSteadyVelSwerve.LandOrientAcceValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftAcceSwerve":
                                        AircraftAcceSwerve aircraftAcceSwerve = new AircraftAcceSwerve();
                                        aircraftAcceSwerve = (AircraftAcceSwerve)item;
                                        strtemp = string.Format("{0},{1},{2},{3}\n", AircraftSimTrackNameFileSave.AirTurnAcc.ToString(), Math.Round(aircraftAcceSwerve.NavOrientChageValue * RATDEG, 15), aircraftAcceSwerve.LandOrientAcceValue, aircraftAcceSwerve.VelchageValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;

                                    case "AircraftPointRun":
                                        AircraftPointRun aircraftPointRun = new AircraftPointRun();
                                        aircraftPointRun = (AircraftPointRun)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4}\n", AircraftSimTrackNameFileSave.AirWayPoint.ToString(), Math.Round(aircraftPointRun.Longitude * RATDEG, 15), Math.Round(aircraftPointRun.Latitude * RATDEG, 15), aircraftPointRun.Height, aircraftPointRun.LandOrientAcceValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftCombRun":
                                        AircraftCombRun aircraftCombRun = new AircraftCombRun();
                                        aircraftCombRun = (AircraftCombRun)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4},{5}\n", AircraftSimTrackNameFileSave.AirCombined.ToString(), Math.Round(aircraftCombRun.NavOrientChageValue * RATDEG, 15), aircraftCombRun.LandOrientAcceValue, aircraftCombRun.DeltaHeight, aircraftCombRun.DotHeight, aircraftCombRun.VelchageValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftLinePitch":
                                        AircraftLinePitch aircraftLinePitch = new AircraftLinePitch();
                                        aircraftLinePitch = (AircraftLinePitch)item;
                                        strtemp = string.Format("{0},{1},{2}\n", AircraftSimTrackNameFileSave.AirLinePitch.ToString(), aircraftLinePitch.RunTime, Math.Round(aircraftLinePitch.PitchChageValue * RATDEG, 15));
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftLineLandRoll":
                                        AircraftLineLandRoll aircraftLineLandRoll = new AircraftLineLandRoll();
                                        aircraftLineLandRoll = (AircraftLineLandRoll)item;
                                        strtemp = string.Format("{0},{1},{2}\n", AircraftSimTrackNameFileSave.AirLineRoll.ToString(), aircraftLineLandRoll.RunTime, Math.Round(aircraftLineLandRoll.LandRollChageValue * RATDEG, 15));
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftLineNav":
                                        AircraftLineNav aircraftLineNav = new AircraftLineNav();
                                        aircraftLineNav = (AircraftLineNav)item;
                                        strtemp = string.Format("{0},{1},{2}\n", AircraftSimTrackNameFileSave.AirLineYaw.ToString(), aircraftLineNav.RunTime, Math.Round(aircraftLineNav.NavChageValue * RATDEG, 15));
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "AircraftStopRun":
                                        AircraftStopRun aircraftStopRun = new AircraftStopRun();
                                        aircraftStopRun = (AircraftStopRun)item;
                                        strtemp = string.Format("{0},{1},{2}\n", AircraftSimTrackNameFileSave.AirHalt.ToString(), aircraftStopRun.StopWay.StopSelect, aircraftStopRun.StopWay.StopValue);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    #endregion

                                    #region 船轨迹
                                    case "BoatStartSetting":
                                        BoatStartSetting boatStartSet = new BoatStartSetting();
                                        boatStartSet = (BoatStartSetting)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4},{5}\n", BoatSimTrackNameFileSave.Ref.ToString(), Math.Round(boatStartSet.Longitude * RATDEG, 15), Math.Round(boatStartSet.Latitude * RATDEG, 15), boatStartSet.Height, Math.Round(boatStartSet.NavOrientation * RATDEG, 15), boatStartSet.Velocity);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "BoatSteadyVelocityLine":
                                        BoatSteadyVelocityLine boatSteadyVelocityLine = new BoatSteadyVelocityLine();
                                        boatSteadyVelocityLine = (BoatSteadyVelocityLine)item;
                                        strtemp = string.Format("{0},{1},{2},{3}\n", BoatSimTrackNameFileSave.BoatLineConst.ToString(), boatSteadyVelocityLine.RunWay.RunSelect, boatSteadyVelocityLine.RunWay.RunValue, boatSteadyVelocityLine.OceanWaveLevel);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "BoatAccelerateLine":
                                        BoatAccelerateLine boatAccelerateLine = new BoatAccelerateLine();
                                        boatAccelerateLine = (BoatAccelerateLine)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4},{5}\n", BoatSimTrackNameFileSave.BoatLineAcc.ToString(), boatAccelerateLine.AccelerateWay.AccelerateSelect, boatAccelerateLine.AccelerateWay.AccelerateValue, boatAccelerateLine.VelChangeWay.VelChangeSelect, boatAccelerateLine.VelChangeWay.VelChangeValue, boatAccelerateLine.OceanWaveLevel);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "BoatSteadyVelSwerve":
                                        BoatSteadyVelSwerve boatSteadyVelSwerve = new BoatSteadyVelSwerve();
                                        boatSteadyVelSwerve = (BoatSteadyVelSwerve)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4}\n", BoatSimTrackNameFileSave.BoatTurnConst.ToString(), boatSteadyVelSwerve.OrientChageWay.OrientationChangeSelect, boatSteadyVelSwerve.OrientChageWay.OrientationChangeValue, boatSteadyVelSwerve.SwerveRadius, boatSteadyVelSwerve.OceanWaveLevel);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "BoatPointRun":
                                        BoatPointRun boatPointRun = new BoatPointRun();
                                        boatPointRun = (BoatPointRun)item;
                                        strtemp = string.Format("{0},{1},{2},{3},{4},{5}\n", BoatSimTrackNameFileSave.BoatPoint.ToString(), Math.Round(boatPointRun.Longitude * RATDEG, 15), Math.Round(boatPointRun.Latitude * RATDEG, 15), boatPointRun.Height, boatPointRun.SwerveRadius, boatPointRun.OceanWaveLevel);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    case "BoatStopRun":
                                        BoatStopRun boatStopRun = new BoatStopRun();
                                        boatStopRun = (BoatStopRun)item;
                                        strtemp = string.Format("{0},{1},{2},{3}\n", BoatSimTrackNameFileSave.BoatHalt.ToString(), boatStopRun.StopWay.StopSelect, boatStopRun.StopWay.StopValue, boatStopRun.OceanWaveLevel);
                                        File.AppendAllText(SaveFilePath, strtemp);
                                        break;
                                    #endregion
                                   
                                }
                                break;//如果判断UID存在则跳出循环
                            }
                        }
                    }
                    #endregion

                    #region 保存限值文件
                    SaveTrackLimitValueFile(SavePath, SaveName);
                    #endregion

                    //#region 保存波浪等级文件
                    //if (FlagModel == "BoatTrack")
                    //{
                    //    SaveWaveLevelFile(SavePath, SaveName);
                    //}
                    //#endregion
                }
                catch (Exception)
                {
                    MessageBox.Show("保存场景出错!!");
                }
            }
        }
        #endregion 

        #region///PropertyChanged接口
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string Info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(Info));
            }
        }
        #endregion
    }
}
